package com.demo.simpletestapp.utils;

import android.app.ActivityOptions;
import android.graphics.Rect;

import com.blankj.utilcode.util.ScreenUtils;

import java.lang.reflect.Method;

public class Utils {
    private static Method setLaunchWindowingModeMethod;

    public static ActivityOptions getFreeformWindowModeLaunchOptions(){
        try {
            if (setLaunchWindowingModeMethod == null){
                setLaunchWindowingModeMethod = ActivityOptions.class.getMethod("setLaunchWindowingMode", int.class);
            }
            if (setLaunchWindowingModeMethod != null){
                ActivityOptions options = ActivityOptions.makeBasic();
                setLaunchWindowingModeMethod.invoke(options, 5);

                int right = ScreenUtils.getScreenWidth() - 40;
                int bottom = ScreenUtils.getScreenHeight() - 120;
                options.setLaunchBounds(new Rect(40, 0, right, bottom));
                return options;

//                修改后，新的方式计算屏幕大小
//                //                窗口高 = 屏幕高度-状态栏高度-导航栏高度
//                int windowHeight = ScreenUtils.getScreenHeight() - QMUIDisplayHelper.getActionBarHeight(ActivityUtils.getTopActivity()) - QMUIDisplayHelper.getNavMenuHeight(ActivityUtils.getTopActivity());
////                窗口宽 = 高 / 1.5
//                int windowWidth = (int) (windowHeight/1.5);
////                left = (屏幕宽度 - 窗口宽) / 2
//                int left = (ScreenUtils.getScreenWidth() - windowWidth)/2;
//                int top = 0;
////                right = left + 窗口宽
//                int right = ScreenUtils.getScreenWidth() - 40;
////                bottom = top + 窗口高
//                int bottom = top + windowHeight;
//                options.setLaunchBounds(new Rect(left, top, right, bottom));
//                return options;

////                int statusBarHeight = (int) ActivityUtils.getTopActivity().getResources().getDimension(com.android.internal.R.dimen.status_bar_height);
//                int statusBarHeight = QMUIDisplayHelper.getActionBarHeight(ActivityUtils.getTopActivity());
////                int displayHeight = display.getHeight();//已经去除了导航栏高度
//                //                窗口高 = 屏幕高度-状态栏高度-导航栏高度
//                int displayHeight = ScreenUtils.getScreenHeight() - QMUIDisplayHelper.getActionBarHeight(ActivityUtils.getTopActivity()) - QMUIDisplayHelper.getNavMenuHeight(ActivityUtils.getTopActivity());
//                int taskHeight = displayHeight - statusBarHeight;
//                Point realSize = new Point();
//                Display display = (ActivityUtils.getTopActivity()).getWindowManager().getDefaultDisplay();
//                display.getRealSize(realSize);
//                int screenWidth = realSize.x;
//                int screenHeight = realSize.y;
//                float ratio = screenHeight * 1f / screenWidth;
//                int taskWidth = (int) (taskHeight / ratio);
//                int left = (screenWidth - taskWidth) / 2;
//                int top = 0;
//                options.setLaunchBounds(new Rect(left, top, left + taskWidth, top + taskHeight));
////                return new Rect(left,0, left + taskWidth, top + taskHeight);
//                return options;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
